//
//  JTTabBarController.m
//  百思不得姐
//
//  Created by sun lei on 16/11/3.
//  Copyright © 2016年 BeiJingWangDianJieTong. All rights reserved.
//

#import "JTTabBarController.h"
#import "JTTabBar.h"
#import "JTEssenceViewController.h"
#import "JTNewViewController.h"
#import "JTFollowViewController.h"
#import "JTMeViewController.h"
#import "JTNavigationController.h"

@interface JTTabBarController ()

@end

@implementation JTTabBarController

#pragma mark - 初始化
- (void)viewDidLoad {
    [super viewDidLoad];
    
    /**** 设置所有UITabBarItem的文字属性 ****/
    [self setupItemTitleTextAttributes];
    
    /**** 添加子控制器 ****/
    [self setupChildViewControllers];
    
    /**** 更换TabBar ****/
    [self setupTabBar];
}


/**
 * 设置所有UITabBarItem的文字属性
 */
- (void)setupItemTitleTextAttributes {
    
    UITabBarItem *item = [UITabBarItem appearance];// appearance:外表 外观；调用的方法必须有 UI_APPEARANCE_SELECTOR 这个宏
    
    // 1.设置Normal状态下的文字属性
    NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary];
    [normalAttrs setObject:[UIFont systemFontOfSize:14] forKey:NSFontAttributeName];
    [normalAttrs setObject:[UIColor grayColor] forKey:NSForegroundColorAttributeName];
    [item setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
    
    // 2.设置Selected状态下的文字属性(NSFontAttributeName字体不起作用)
    NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
    [selectedAttrs setObject:[UIColor darkGrayColor] forKey:NSForegroundColorAttributeName];
    [item setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
}


/**
 * 添加子控制器
 */
- (void)setupChildViewControllers {
    
    [self setUpOneChildViewController:[[JTNavigationController alloc] initWithRootViewController:[[JTEssenceViewController alloc] init]]  title:@"精华" image:@"tabBar_essence_icon" selectedImage:@"tabBar_essence_click_icon"];
    
    [self setUpOneChildViewController:[[JTNavigationController alloc] initWithRootViewController:[[JTNewViewController alloc] init]] title:@"最新" image:@"tabBar_new_icon" selectedImage:@"tabBar_new_click_icon"];
    
    [self setUpOneChildViewController:[[JTNavigationController alloc] initWithRootViewController:[[JTFollowViewController alloc] init]] title:@"关注" image:@"tabBar_friendTrends_icon" selectedImage:@"tabBar_friendTrends_click_icon"];
    
    [self setUpOneChildViewController:[[JTNavigationController alloc] initWithRootViewController: [[JTMeViewController alloc] init]] title:@"我" image:@"tabBar_me_icon" selectedImage:@"tabBar_me_click_icon"];
}

/**
 * 初始化一个子控制器
 *
 * @param vc            子控制器
 * @param title         标题
 * @param image         图标
 * @param selectedImage 选中的图标
 */
- (void)setUpOneChildViewController:(UIViewController *)vc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage {
    
    /*
     arc4random_uniform(255):直接生成一个小于等于255的数
     arc4random() % 255:生成一个无穷大的数，然后通过求余运算得到一个[0, 255)的数
     */
    
    [vc.tabBarItem setTitle:title];
    if (image.length) { // 图片名有具体值(至于为什么不用判断字符串长度大于0。要知道若字符串为空串，它的length是0：而0正好代表BOOL值中的NO)
        
        [vc.tabBarItem setImage:[UIImage imageNamed:image]];
    }
    if (selectedImage.length) {
        
        [vc.tabBarItem setSelectedImage:[UIImage imageNamed:selectedImage]];
    }
    [self addChildViewController:vc];
}

/**
 * 更换TabBar
 */
- (void)setupTabBar {
    
    [self setValue:[[JTTabBar alloc] init] forKey:@"tabBar"]; // 使用KVC设置tabBar，因为tabBar是只读属性
}

@end
